/*
 * PanelLinearization.java
 *
 * Created on 2008年4月5日, 下午7:14
 */

package GUI;

import Manager.Manager;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.swing.JLabel;
import Global.Global;
import java.awt.*;
/**
 *
 * @author  littlechild
 */
public class PanelLinearization extends javax.swing.JPanel {
    Manager manager;
    PanelCavas drawCavas;
    double contrastScale=100;
    int contrastMax=300;
    boolean isInitiating;
    /** Creates new form PanelLinearization */
    public PanelLinearization(Manager mg,PanelCavas pc) {
        manager=mg;
        drawCavas=pc;
        initComponents();
        init();
    }

    private void init() {
        isInitiating=true;
        this.lightnessText.setText("0");
        this.contrastText.setText("1");
        this.contrastSlide.setMinimum(0);
        this.contrastSlide.setMaximum(contrastMax);
        Dictionary<Integer,JLabel> labels=new Hashtable<Integer,JLabel>();
        labels.put(new Integer(0), new JLabel("0"));
        labels.put(new Integer(contrastMax),new JLabel("5"));
        this.contrastSlide.setLabelTable(labels);
        this.lightnessSlide.setMinimum(-150);
        this.lightnessSlide.setMaximum(150);
        this.lightnessSlide.setValue(0);
        this.contrastSlide.setValue((int)contrastScale);
        isInitiating=false;
    }
    
    private void preview() {
        if(!isInitiating) {
            String paras[]=new String[]{this.lightnessText.getText(),this.contrastText.getText()};
            //System.out.println(paras[0]+";"+paras[1]);
            this.drawCavas.setImage(manager.operate(Global.opLinearization, paras,true));
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lightnessText = new javax.swing.JTextField();
        contrastText = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        BtOk = new javax.swing.JButton();
        lightnessSlide = new javax.swing.JSlider();
        contrastSlide = new javax.swing.JSlider();
        BtCancel = new javax.swing.JButton();

        lightnessText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lightnessTextActionPerformed(evt);
            }
        });

        contrastText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                contrastTextActionPerformed(evt);
            }
        });

        jLabel1.setText("亮度:");

        jLabel2.setText("对比度：");

        BtOk.setText("确定");
        BtOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtOkActionPerformed(evt);
            }
        });

        lightnessSlide.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                lightnessSlideStateChanged(evt);
            }
        });

        contrastSlide.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                contrastSlideStateChanged(evt);
            }
        });

        BtCancel.setText("取消");
        BtCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(BtOk)
                        .addGap(45, 45, 45)
                        .addComponent(BtCancel))
                    .addComponent(contrastSlide, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lightnessSlide, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(76, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lightnessText)
                    .addComponent(contrastText, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
                .addContainerGap(61, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lightnessText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lightnessSlide, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(contrastText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(contrastSlide, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtOk)
                    .addComponent(BtCancel))
                .addGap(25, 25, 25))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void lightnessTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lightnessTextActionPerformed
        // TODO add your handling code here:
        double textValue=Double.parseDouble(this.lightnessText.getText());
        this.lightnessSlide.setValue((int)(textValue));
}//GEN-LAST:event_lightnessTextActionPerformed

    private void contrastSlideStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_contrastSlideStateChanged
        // TODO add your handling code here:
        this.contrastText.setText(String.valueOf((double)(this.contrastSlide.getValue()/contrastScale)));
        preview();
    }//GEN-LAST:event_contrastSlideStateChanged

    private void contrastTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_contrastTextActionPerformed
        // TODO add your handling code here:
        double textValue=Double.parseDouble(this.contrastText.getText());
        this.contrastSlide.setValue((int)(textValue*contrastScale));
    }//GEN-LAST:event_contrastTextActionPerformed

    private void lightnessSlideStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_lightnessSlideStateChanged
        // TODO add your handling code here:
        this.lightnessText.setText(String.valueOf(this.lightnessSlide.getValue()));
        preview();
    }//GEN-LAST:event_lightnessSlideStateChanged

    private void BtCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtCancelActionPerformed
        // TODO add your handling code here:
        this.drawCavas.setImage(manager.operate(Global.opNone, null));
        Container c=this.getParent();
        CommonFunc.disposeFrame(this);
}//GEN-LAST:event_BtCancelActionPerformed

    private void BtOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtOkActionPerformed
        // TODO add your handling code here:
        String paras[]=new String[]{this.lightnessText.getText(),this.contrastText.getText()};
        this.drawCavas.setImage(manager.operate(Global.opLinearization, paras));
        CommonFunc.disposeFrame(this);
    }//GEN-LAST:event_BtOkActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtCancel;
    private javax.swing.JButton BtOk;
    private javax.swing.JSlider contrastSlide;
    private javax.swing.JTextField contrastText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSlider lightnessSlide;
    private javax.swing.JTextField lightnessText;
    // End of variables declaration//GEN-END:variables
    
}
